import math
print(math.sqrt(16))  # 4.0（使用模块名.函数名）






from random import randint
print(randint(1, 10))  # 生成1-10随机整数（直接使用函数名）











import datetime as dt
now = dt.datetime.now()  # 使用别名缩短调用
print(now.year)














from math import *  # 所有函数直接可用
print(sin(pi/2))    # 1.0（容易造成命名冲突）







import math

# 基本运算
print(math.ceil(4.3))    # 5（向上取整）
print(math.floor(4.9))   # 4（向下取整）
print(math.fabs(-5))     # 5.0（绝对值）

# 对数与幂
print(math.pow(2, 3))    # 8.0（2的3次方）
print(math.log(100, 10)) # 2.0（以10为底100的对数）

# 三角函数（参数为弧度）
print(math.sin(math.pi/2))  # 1.0
print(math.degrees(math.pi)) # 180.0（弧度转角度）

# 常量
print(math.pi)    # 3.141592653589793
print(math.e)     # 2.718281828459045











import random

# 基本随机数
print(random.random())  # [0.0, 1.0)之间的随机浮点数
print(random.uniform(1.5, 3.5))  # 指定范围内的随机浮点数

# 随机整数
print(random.randint(1, 10))     # 包含两端点
print(random.randrange(0, 100, 5)) # 0-100之间步长5的随机数

# 序列操作
colors = ['red', 'green', 'blue']
print(random.choice(colors))     # 随机选择1个元素
random.shuffle(colors)           # 原地打乱列表
print(colors)
print(random.sample(colors, 2))  # 随机选取2个不重复元素

# 随机种子（重现随机序列）
random.seed(42)  # 设置种子后，随机序列固定
print([random.randint(1,10) for _ in range(3)])  # 每次运行输出相同






from datetime import datetime, date, time, timedelta

# 获取当前时间
now = datetime.now()
print(f"当前时间: {now}")          # 2023-08-20 14:30:25.123456
print(f"格式化输出: {now.strftime('%Y-%m-%d %H:%M:%S')}")

# 创建特定日期
d = date(2023, 8, 20)
t = time(14, 30)
dt = datetime.combine(d, t)
print(dt)  # 2023-08-20 14:30:00

# 时间运算
tomorrow = now + timedelta(days=1)
print(f"明天此时: {tomorrow}")

# 时间戳转换
timestamp = now.timestamp()  # 转为时间戳
print(datetime.fromtimestamp(timestamp))  # 时间戳转回datetime

# 时区处理（需要pytz模块）
from pytz import timezone
tz = timezone('Asia/Shanghai')
print(datetime.now(tz))  # 带时区的时间



import random
import string

def generate_password(length=12):
    """生成包含大小写字母、数字和特殊字符的密码"""
    chars = string.ascii_letters + string.digits + "!@#$%^&*"
    while True:
        password = ''.join(random.choice(chars) for _ in range(length))
        # 密码强度检查（必须包含各类字符）
        if (any(c.islower() for c in password)
            and any(c.isupper() for c in password)
            and any(c.isdigit() for c in password)
            and any(c in "!@#$%^&*" for c in password)):
            return password

print(generate_password())  # 类似：J7k#p2X9!Lm1

from datetime import datetime, timedelta


def countdown(target_date):
    """计算距离目标日期的倒计时"""
    now = datetime.now()
    delta = target_date - now

    if delta.total_seconds() <= 0:
        return "目标日期已过！"

    days = delta.days
    hours, remainder = divmod(delta.seconds, 3600)
    minutes, seconds = divmod(remainder, 60)

    return f"剩余时间: {days}天 {hours}小时 {minutes}分钟 {seconds}秒"


# 计算距离明年春节的倒计时
spring_festival = datetime(2024, 2, 10)  # 2024年春节
print(countdown(spring_festival))









import math
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-2*math.pi, 2*math.pi, 200)
y_sin = [math.sin(i) for i in x]
y_cos = [math.cos(i) for i in x]

plt.plot(x, y_sin, label='sin(x)')
plt.plot(x, y_cos, label='cos(x)')
plt.legend()
plt.title("Trigonometric Functions")
plt.show()







import random
import string

def generate_password(length=12):
    """生成包含大小写字母和数字的随机密码"""
    characters = string.ascii_letters + string.digits  # 字母+数字
    password = ''.join(random.choice(characters) for _ in range(length))
    return password

# 生成10个字符的密码
print(generate_password(10))  # 示例输出：'J7k2X9Lm1p'


def generate_strong_password(length=12):
    """生成必须包含大小写字母、数字和特殊字符的密码"""
    # 定义字符集
    lower = string.ascii_lowercase
    upper = string.ascii_uppercase
    digits = string.digits
    special = '!@#$%^&*'

    # 确保每种类型至少有一个字符
    password = [
        random.choice(lower),
        random.choice(upper),
        random.choice(digits),
        random.choice(special)
    ]

    # 填充剩余字符
    all_chars = lower + upper + digits + special
    password.extend(random.choice(all_chars) for _ in range(length - 4))

    # 打乱顺序
    random.shuffle(password)
    return ''.join(password)


print(generate_strong_password())  # 示例输出：'T8#k3P$m9!L'






import secrets
import string

def generate_secure_password(length=16):
    """生成加密安全的随机密码"""
    alphabet = string.ascii_letters + string.digits + '!@#$%^&*'
    while True:
        password = ''.join(secrets.choice(alphabet) for _ in range(length))
        # 强制复杂性要求
        if (any(c.islower() for c in password)
            and any(c.isupper() for c in password)
            and any(c.isdigit() for c in password)
            and any(c in '!@#$%^&*' for c in password)):
            return password

print(generate_secure_password())  # 示例输出：'vA7#mK9*P2!xY5$q'